package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import domain.DomainException;
import domain.gbshaasrode.spreadsheet.GBSHaasrodeSpreadSheetService;
import domain.gbshaasrode.spreadsheet.GBSOpvangSpreadSheet;
import domain.gbshaasrode.spreadsheet.GBSOpvangWorkSheetLeerlingen;
import domain.gbshaasrode.spreadsheet.GBSOpvangWorkSheetOpvang;

public class View extends JFrame {

	private static final long serialVersionUID = 1L;

	private JPanel inputPanel;
	private JPanel fninputPanel;
	private JPanel lninputPanel;
	private JPanel minputPanel;
	private JPanel yinputPanel;
	private JPanel fileinputPanel;
	private JLabel nameLabel;
	private JTextField nameTextField;
	private JLabel firstnameLabel;
	private JTextField firstnameTextField;
	private JLabel monthLabel;
	private JTextField monthTextField;
	private JLabel yearLabel;
	private JTextField yearTextField;
	private JLabel filenameLabel;
	private JTextField filenameTextField;

	private JPanel buttonPanel;
	private JButton saveButton;

	private JTextArea messageArea;

	public View() {
		super("GBS Haasrode Opvang");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		initComponents();
		fillPanels();
		addComponents();
		pack();
	}

	private void initComponents() {
		inputPanel = new JPanel();
		fninputPanel = new JPanel();
		lninputPanel = new JPanel();
		minputPanel = new JPanel();
		yinputPanel = new JPanel();
		fileinputPanel = new JPanel();
		nameLabel = new JLabel("Achternaam: ");
		nameTextField = new JTextField(10);
		nameTextField.setToolTipText("Achternaam");
		firstnameLabel = new JLabel("Voornaam: ");
		firstnameTextField = new JTextField(10);
		firstnameTextField.setToolTipText("Voornaam");
		monthLabel = new JLabel("Maand (1-12): ");
		monthTextField = new JTextField(10);
		monthTextField.setToolTipText("Maand");		
		yearLabel = new JLabel("Jaar (yyyy): ");
		yearTextField = new JTextField(10);
		yearTextField.setToolTipText("Jaar");
		filenameLabel = new JLabel("bestand*: ");
		filenameTextField = new JTextField(10);
		filenameTextField.setToolTipText("bestand");


		buttonPanel = new JPanel();
		saveButton = new JButton("Opslaan");
		saveButton.addActionListener(new ButtonListener());

		messageArea = new JTextArea();
	}

	private void fillPanels() {
		fninputPanel.setLayout(new BoxLayout(fninputPanel, BoxLayout.X_AXIS));
		fninputPanel.add(firstnameLabel);
		fninputPanel.add(firstnameTextField);
		lninputPanel.setLayout(new BoxLayout(lninputPanel, BoxLayout.X_AXIS));
		lninputPanel.add(nameLabel);
		lninputPanel.add(nameTextField);
		minputPanel.setLayout(new BoxLayout(minputPanel, BoxLayout.X_AXIS));
		minputPanel.add(monthLabel);
		minputPanel.add(monthTextField);
		yinputPanel.setLayout(new BoxLayout(yinputPanel, BoxLayout.X_AXIS));
		yinputPanel.add(yearLabel);
		yinputPanel.add(yearTextField);
		fileinputPanel.setLayout(new BoxLayout(fileinputPanel, BoxLayout.X_AXIS));
		fileinputPanel.add(filenameLabel);
		fileinputPanel.add(filenameTextField);
		
		inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS));
		inputPanel.add(fninputPanel);
		inputPanel.add(lninputPanel);
		inputPanel.add(minputPanel);
		inputPanel.add(yinputPanel);
		inputPanel.add(fileinputPanel);

		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
	}

	private void addComponents() {
		getContentPane().setLayout(
				new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
		getContentPane().add(inputPanel);
		getContentPane().add(saveButton);
		getContentPane().add(messageArea);
	}

	private class ButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			if (arg0.getSource().equals(saveButton)) {
				try{
					saveInformation();
					messageArea.setText("De gegevens werden met succes opgeslagen");
				} catch(DomainException e) {
					messageArea.setText("Er is iets foutgelopen");
				}
			} 
		}
	}
	
	private void saveInformation() throws DomainException{
		String fn = firstnameTextField.getText();
		String ln = nameTextField.getText();
		String month = monthTextField.getText();
		String year = yearTextField.getText();
		String file = filenameTextField.getText();
		
		if(month.equals("")){
			month += Calendar.getInstance().get(Calendar.MONTH)+1;
		}
		System.out.println("month: "+month);
		int m = Integer.parseInt(month);
		if(year.equals("")){
			year += Calendar.getInstance().get(Calendar.YEAR);
		}
		System.out.println("year: "+year);
		int y = Integer.parseInt(year);
		
		File bestand = new File(file);
		PrintWriter pw = null;
		try {
			 pw = new PrintWriter(bestand);			 
		} catch (FileNotFoundException e) {
			throw new DomainException("bestand niet gevonden");
		}
		
		GBSHaasrodeSpreadSheetService service = new GBSHaasrodeSpreadSheetService();
		GBSOpvangSpreadSheet ss = new GBSOpvangSpreadSheet(service);		
		GBSOpvangWorkSheetLeerlingen leerlingen = new GBSOpvangWorkSheetLeerlingen(service, ss);
		GBSOpvangWorkSheetOpvang opvang = new GBSOpvangWorkSheetOpvang(service, ss);
		ArrayList<Integer> llnrs = leerlingen.getNrsFor(fn, ln);
		if(llnrs.size() == 1){
			String ll = leerlingen.toString(llnrs.get(0));
			ll += "\n";
			ll += opvang.toString(llnrs.get(0), m, y);
			pw.println(ll);
			//System.out.println(ll);
		} else{
			for(Integer i: llnrs){
				String lln = leerlingen.toString(i);
				lln += opvang.getTeBetalen(i, m, y);
				pw.println(lln);
				//System.out.println(lln);
			}
		}		
		if(pw != null){
			pw.close();
		}
	}

}
